package com.atguigu.tingshu.account.receiver;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AccountReceiver {
    @Autowired
    private UserAccountService userAccountService;
@SneakyThrows
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = MqConst.QUEUE_USER_REGISTER,durable = "true",autoDelete = "false"),
        exchange = @Exchange(value = MqConst.EXCHANGE_USER),
        key = MqConst.ROUTING_USER_REGISTER
))
    public void userRegister(Long userId, Message message, Channel channel){

        System.out.println("用户注册成功，用户id："+userId);
        try {
            if (userId == null){
                userAccountService.initUserAccount(userId);
            }

        }catch (Exception e){
            throw new RuntimeException(e);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}

